<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/vue.js"></script>
	</head>
	<body>
		<div id="root">
			<h2>人员信息</h2>
			<input type="text" placeholder="请输入名称" v-model="keyword" />
			<button @click="order = 0">原顺序</button>
			<button @click="order = 1">升序</button>
			<button @click="order = 2">降序</button>
			<ul>
				<!-- <li v-for="p in persons" :key="p.id"> -->
				<li v-for="(p,index) of filterPersons" :key="index">
					{{p.name}}-{{p.age}}
				</li>
			</ul>
		</div>
	</body>
	<script>
		// #region
		new Vue({
			el: '#root',
			data() {
				return {
					persons: [{
							id: '001',
							name: '张三',
							age: 13
						},
						{
							id: '002',
							name: '李四',
							age: 11
						},
						{
							id: '003',
							name: '王五',
							age: 12
						}
					],
					keyword: '',
					order:0, //0原顺序 1升序 2降序
					// filterPersons:[]
				}
			},
			// watch:{
			// 	keyword:{
			// 		immediate:true,
			// 		handler(val){
			// 			this.filterPersons = this.persons.filter((p) => {
			// 				return p.name.includes(val)
			// 			})
			// 		}
			// 	}
			// }
			computed: {
				filterPersons() {
					let arr = this.persons.filter((p) => {
						return p.name.includes(this.keyword)
					})
					if(this.order){
						arr.sort((a,b)=>{
							return this.order === 1 ? a.age - b.age : b.age - a.age
						})
					}
					return arr
				}
			}
		})
		//#endregion
	</script>
</html>

